#!/usr/bin/env python
# -*- coding: utf-8 -*- 

import os
import sys
import wx
import mytreectrl
import mymenu
import logindialog
import userdoc

class MainWindow(wx.Frame):
    """wxProject MainFrame."""
    def __init__(self, parent, icopath, userdoc, title):
        """Create the wxProject MainFrame."""
        wx.Frame.__init__(self, parent, title=title, size=(800, 650),
                          style=wx.DEFAULT_FRAME_STYLE|wx.NO_FULL_REPAINT_ON_RESIZE|wx.MAXIMIZE)

        self.SetIcon(wx.Icon(icopath,wx.BITMAP_TYPE_ICO))
        
        self.menu = mymenu.MyMenu(self)
        
        # Create the splitter window.
        splitter = wx.SplitterWindow(self, style=wx.NO_3D|wx.SP_3D)
        splitter.SetMinimumPaneSize(1)

        # Create the tree on the left.
        self.tree = mytreectrl.MyTreeCtrl(splitter, userdoc)
        
        # Create the editor on the right.
        self.editor = wx.TextCtrl(splitter, style=wx.TE_MULTILINE)

        userdoc.set_editor(self.editor)
        
        # Install the tree and the editor.
        splitter.SplitVertically(self.tree, self.editor)
        splitter.SetSashPosition(180, True)

        # Some global state variables.
        self.Show(True)

class MyApp(wx.App):
    def OnInit(self):
        self.apppath = os.path.dirname(os.path.abspath(sys.argv[0]))
        self.icopath = os.path.join(self.apppath, '../icons','everydoc.ico')
        return True
    
    def ShowMainFrame(self):
        frame = MainWindow(None, self.icopath, self.userdoc, self.account + '@docs.google.com')
        
    def GetAccountInfo(self):
        self.account = ""
        self.password = ""
        userinfo = ['', '']
        login = logindialog.LoginDialog(None, self.icopath, 'Login', userinfo)
        self.account = userinfo[0]
        self.password = userinfo[1]
        if len(self.account) == 0 or len(self.password) == 0:
          return False
       
        self.userdoc = userdoc.UserDoc(self.account, self.password)
        return self.userdoc.Init()
    
if __name__ == '__main__':    
    app = MyApp(None)
    if app.GetAccountInfo() == True:
        app.ShowMainFrame()
        app.MainLoop()
